﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint.Utilities;

namespace SharePoint2010Poll
{
    public static class PollHelper
    {
        internal static void CreatePoll(PollsItem NewPoll)
        {
            using (SPMonitoredScope sc = new SPMonitoredScope("SharePoint 2010 User Poll - CreatePoll"))
            {
                using (EntitiesDataContext dc = new EntitiesDataContext(SPContext.Current.Web.Url))
                {
                    //Check if poll already exist and delete it first
                    DeletePoll(NewPoll.PollId);
                    dc.Polls.InsertOnSubmit(NewPoll);
                    dc.SubmitChanges();
                }
            }
        }

        internal static void DeletePoll(string PollID)
        {
            using (SPMonitoredScope sc = new SPMonitoredScope("SharePoint 2010 User Poll - DeletePoll"))
            {
                using (EntitiesDataContext dc = new EntitiesDataContext(SPContext.Current.Web.Url))
                {
                    var pollItems = from polls in dc.Polls
                                    where polls.PollId == PollID
                                    select polls;

                    foreach (PollsItem poll in pollItems)
                    {
                        dc.Polls.DeleteOnSubmit(poll);
                        DeleteUserResponses(PollID);
                    }

                    dc.SubmitChanges();
                }
            }
        }

        internal static PollsItem GetPoll(string PollID)
        {
            using (SPMonitoredScope sc = new SPMonitoredScope("SharePoint 2010 User Poll - GetPoll"))
            {
                PollsItem poll = (PollsItem)CacheHelper.GetFromCache(PollID, CachedObjectType.PollDataCache);

                if (poll == null)
                {
                    using (EntitiesDataContext dc = new EntitiesDataContext(SPContext.Current.Web.Url))
                    {
                        var pollItems = from polls in dc.Polls
                                        where polls.PollId == PollID
                                        select polls;

                        if (pollItems.Count() > 0)
                        {
                            poll = pollItems.First<PollsItem>();
                            CacheHelper.SetInCache(PollID, CachedObjectType.PollDataCache, poll);
                        }
                    }
                }

                return poll;
            }
        }
        internal static List<string> GetAnswers(string PollAnswerString)
        {
            return PollAnswerString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList<string>();
        }

        internal static List<PollUserResponsesItem> GetUserResponses(string PollID)
        {
            using (SPMonitoredScope sc = new SPMonitoredScope("SharePoint 2010 User Poll - GetUserResponses"))
            {
                List<PollUserResponsesItem> pollUserResponseItemsList =
                  (List<PollUserResponsesItem>)CacheHelper.GetFromCache(PollID, CachedObjectType.PollResponsesCache);

                if (pollUserResponseItemsList == null)
                {
                    using (EntitiesDataContext dc = new EntitiesDataContext(SPContext.Current.Web.Url))
                    {
                        var pollUserResponseItems = from pollUserResponses in dc.PollUserResponses
                                                    where pollUserResponses.PollId == PollID
                                                    select pollUserResponses;

                        if (pollUserResponseItems.Count() > 0)
                        {
                            pollUserResponseItemsList = pollUserResponseItems.ToList<PollUserResponsesItem>();
                            CacheHelper.SetInCache(PollID, CachedObjectType.PollResponsesCache, pollUserResponseItemsList);
                        }
                    }
                }

                return pollUserResponseItemsList;
            }
        }

        private static void DeleteUserResponses(string PollID)
        {
            using (SPMonitoredScope sc = new SPMonitoredScope("SharePoint 2010 User Poll - DeleteUserResponses"))
            {
                using (EntitiesDataContext dc = new EntitiesDataContext(SPContext.Current.Web.Url))
                {
                    var pollUserResponseItems = from pollUserResponses in dc.PollUserResponses
                                                where pollUserResponses.PollId == PollID
                                                select pollUserResponses;

                    foreach (PollUserResponsesItem response in pollUserResponseItems)
                    {
                        dc.PollUserResponses.DeleteOnSubmit(response);
                    }

                    dc.SubmitChanges();
                }
            }
        }

        internal static void SaveUserResponse(PollUserResponsesItem ResponseItem)
        {
            using (SPMonitoredScope sc = new SPMonitoredScope("SharePoint 2010 User Poll - SaveUserResponse"))
            {
                using (EntitiesDataContext dc = new EntitiesDataContext(SPContext.Current.Web.Url))
                {
                    dc.PollUserResponses.InsertOnSubmit(ResponseItem);
                    dc.SubmitChanges();

                    // Invalidate Poll response cache as a new person has just responded to the poll
                    CacheHelper.InvalidateCache(ResponseItem.PollId, CachedObjectType.PollResponsesCache);
                    // Invalidate current users response status
                    CacheHelper.InvalidateCache((ResponseItem.UserId + ResponseItem.PollId), CachedObjectType.PollUserResponseCache);
                }
            }
        }

        internal static bool HasUserResponded(string UserID, string PollID)
        {
            using (SPMonitoredScope sc = new SPMonitoredScope("SharePoint 2010 User Poll - HasUserResponded"))
            {
                bool bHasUserResponded;

                object obj = CacheHelper.GetFromCache((UserID + PollID), CachedObjectType.PollUserResponseCache);

                if (obj != null)
                {
                    bHasUserResponded = (bool)obj;
                }
                else
                {
                    using (EntitiesDataContext dc = new EntitiesDataContext(SPContext.Current.Web.Url))
                    {
                        var pollResponseItems = from pollresponses in dc.PollUserResponses
                                                where pollresponses.PollId == PollID &&
                                                pollresponses.UserId == UserID
                                                select pollresponses;

                        if (pollResponseItems.Count() > 0)
                        {
                            bHasUserResponded = true;
                        }
                        else
                            bHasUserResponded = false;
                    }
                }

                CacheHelper.SetInCache((UserID + PollID), CachedObjectType.PollUserResponseCache, bHasUserResponded);

                return bHasUserResponded;
            }
        }
    }
}
